// 引入 qqmap.js
import qqmap from '../../../utils/qqmap'
// map.js
Page({
  data: {
    latitude: 40.086757,
    longitude: 116.328634,
    // 定义枚举
    fuText: {
      1: '受理中',
      2: '上门中',
      3: '已完成',
      0: '已取消',
    },
    fuClass: {
      1: 'info',
      2: 'success',
      3: 'complet',
      0: 'cancel'
    },
    //
    markers: [
      {
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
  },
  onLoad({ id }) {
    // console.log(id);
    this.getRepairDetail(id)
    this.createPolyLine()
    this.setData({
      repairId: id
    })
  },
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    // console.log(res);
    //将数据保存到data里面
    this.setData({
      ...res.data
    })
  },

  //取消
  openDialogLayer() {
    this.setData({ dialogVisible: true })
  },
  async dialogClose(ev) {
    if (ev.detail === 'confirm') {
      // 请求数据接口
      await wx.http.put(`/cancel/repaire/${this.data.repairId}`)
      // 跳转到报修列表页面
      wx.navigateBack()
    }
  },
  editRepair() {
    wx.navigateTo({
      // repair_id 是全局变量，在前面已经定义
      url: '/repair_pkg/pages/form/index?id=' + this.data.id,
    })
  },
  createPolyLine() {
    //调用距离计算接口
    qqmap.direction({
      mode: 'bicycling',
      // from: '40.086757,116.328634',
      // to: '40.08346500000002,116.33293800000011', 
      from: '40.060539,116.343847', // 传智播客
      to: '40.086757,116.328634', // （示例）云趣园1区
      success: res => {
        let ret = res;
        let coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }

        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      }
    })
  }
})
